import request from '@/utils/request';

interface LoginResponse {
    code: number;
    message: string;
    data: {
        token: string;
    };
}

interface RegisterResponse {
    message: string;
    success: boolean;
}

interface ApiResponse<T> {
    code: number;
    message: string;
    data: T;
}

export interface UserInfo {
    username: string;
    real_name: string;
    phone: string;
    email: string;
    gender: 'male' | 'female';
    birth_date: string;
    status: 'active' | 'inactive';
    role_name: 'admin' | 'teacher' | 'student';
    profile_info: any;
    avatar_url?: string;
}

interface Banner {
    id: number;
    title: string;
    image_url: string;
    route_path: string;
    sort_order: number;
    description: string;
}

export enum CourseLevel {
    BEGINNER = 'beginner',
    INTERMEDIATE = 'intermediate',
    ADVANCED = 'advanced'
}

export const CourseLevelLabel = {
    [CourseLevel.BEGINNER]: '初级',
    [CourseLevel.INTERMEDIATE]: '中级',
    [CourseLevel.ADVANCED]: '高级'
};

interface Course {
    id: number;
    title: string;
    description: string;
    image_url: string;
    teacher_name: string;
    level: CourseLevel;
    price: number;
    original_price: number;
    enrolled_students: number;
    tags: string[];
}

// 添加分类接口类型
interface Category {
    id: number;
    name: string;
    status: string;
    sort_order: number;
    created_at: string;
    course_count: number;
}

// 用户相关接口
export const userAPI = {
    // 登录
    login: (data: { username: string; password: string }) => {
        return request.post<any, LoginResponse>('/login', data);
    },
    
    // 注册
    register: (data: {
        username: string;
        password: string;
        phone: string;
        email: string;
        role: 'teacher' | 'student';
    }) => {
        return request.post<any, RegisterResponse>('/user/register', data);
    },
    
    // 获取用户信息
    getUserInfo: () => {
        return request.get<any, ApiResponse<UserInfo>>('/user/info');
    },
};

// 课程相关接口
export const courseAPI = {
    // 获取课程列表
    getCourses: (categoryId?: number) => {
        const url = categoryId ? `/courses?categoryId=${categoryId}` : '/courses';
        return request.get<any, ApiResponse<Course[]>>(url);
    },
    
    // 获取课程详情
    getCourseDetail: (id: number) => {
        return request.get<any, ApiResponse<Course>>(`/courses/${id}`);
    },
};

// 添加获取分类接口
export const categoryAPI = {
    getCategories: () => {
        return request.get<any, ApiResponse<Category[]>>('/categories');
    }
};

export const publicAPI = {
    getBanners: () => {
        return request.get<any, ApiResponse<Banner[]>>('/public/banners');
    },
}; 